/**
 * @Description:
 * @Author 卞鹏飞 <228443632@qq.com>
 * @create 24/11/24 AM2:45
 */

export type ICommonFunction = (...args: any[]) => any

export type TListenerFunc = (data: { msgId?: string; data: any }) => void

type TChildModelDefault = {
  height?: number
  origin?: string
  [K: string]: any
}
export type TChildOptions = {
  /**
   * 要向父级公开的 gettable 属性的对象。值类型可以是 postMessage 中接受的任何内容。Promise 也可以设置为值或从函数返回。
   */
  model?: TChildModelDefault
}

/**
 * 子 窗口的postmate实例
 */
export type TChildHandshake = {
  /**
   * 销毁
   */
  destroy: () => void

  /**
   * 调用子iframe 方法
   * @param key
   * @param data
   */
  call: (key: string, data: any) => void

  /**
   * 调用子iframe 方法，并拿到返回值
   * @param key
   * @example
   * ```
   * get('xxx').then(res => console.log(res))
   * ```
   */
  get: (key: string) => Promise<any>

  /**
   * 监听
   * @param key
   * @param callback
   */
  on: (key: string, callback: TListenerFunc) => void
}

/**
 * 父 窗口的postmate实例
 */
export type TParentHandshake = {
  /**
   *
   * @param key
   * @param args
   */
  emit: (key: string, ...args: any[]) => void

  /**
   * 微应用id
   */
  msgId?: string
}

/**
 * 父窗口实例入参数
 */
export type TParentOptions = Partial<{
  url: string
  container: HTMLHtmlElement | HTMLIFrameElement
  name: string
  classListArray: string[]
  style: string | (string | Record<string, any>)[] | Record<string, any>
  attrs: Record<string, any>
  model: {
    msgId?: string
  } & Record<string, any>
  onload: ICommonFunction
  onerror: ICommonFunction
  [K: `on${string}`]: ICommonFunction
}>

/**
 * 初始化
 */
export type TParentPostmateMountedFunc = (child: TChildHandshake, ...args: any[]) => any
